package com.storage.system.service.impl;

import java.util.List;

import com.storage.common.core.domain.AjaxResult;
import com.storage.common.core.domain.ServiceResponse;
import com.storage.common.core.page.TableDataInfo;
import com.storage.system.domain.StorageNoticeMessage;
import com.storage.system.domain.StorageUser;
import com.storage.system.mapper.StorageNoticeMessageMapper;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.storage.system.mapper.StorageMessageMapper;
import com.storage.system.domain.StorageMessage;
import com.storage.system.service.IStorageMessageService;
import com.storage.common.core.text.Convert;

/**
 * 用户 服务层实现
 *
 * @author ruoyi
 * @date 2019-09-12
 */
@Service
public class StorageMessageServiceImpl implements IStorageMessageService {

    @Autowired
    private StorageMessageMapper storageMessageMapper;

    @Autowired
    private StorageNoticeMessageMapper storageNoticeMessageMapper;

    /**
     * 查询用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @Override
    public StorageMessage selectStorageMessageById(Long id) {
        return storageMessageMapper.selectStorageMessageById(id);
    }

    /**
     * 查询用户列表
     *
     * @param storageMessage 用户信息
     * @return 用户集合
     */
    @Override
    public ServiceResponse<TableDataInfo> selectStorageMessageList(StorageMessage storageMessage, Integer pageNum, Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        Integer start = (pageNum - 1) * pageSize;
        // 查询查询分页数据
        List<StorageMessage> messages = storageMessageMapper.queryPageList(storageMessage, start, pageSize);
        // 查询totalCount
        Long totalCount = storageMessageMapper.queryPageCount(storageMessage);

        TableDataInfo pageList = new TableDataInfo(messages, totalCount, pageNum, pageSize);
        return new ServiceResponse<>(true, pageList, null);
    }

    /**
     * 新增用户
     *
     * @param storageMessage 用户信息
     * @return 结果
     */
    @Override
    public AjaxResult insertStorageMessage(StorageUser user, StorageMessage storageMessage) {
        if (StringUtils.isBlank(storageMessage.getMessageContent())) {
            return AjaxResult.error("留言内容不能为空");
        }
        storageMessage.setCreateUserId(user.getId());
        storageMessageMapper.insertStorageMessage(storageMessage);
        return AjaxResult.success();
    }

    /**
     * 修改用户
     *
     * @param storageMessage 用户信息
     * @return 结果
     */
    @Override
    public int updateStorageMessage(StorageMessage storageMessage) {
        return storageMessageMapper.updateStorageMessage(storageMessage);
    }

    /**
     * 删除用户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStorageMessageByIds(String ids) {
        return storageMessageMapper.deleteStorageMessageByIds(Convert.toStrArray(ids));
    }

    @Override
    public ServiceResponse<List<StorageNoticeMessage>> selectStorageNoticeMessageList(StorageUser storageUser, StorageNoticeMessage storageNoticeMessage) {
        storageNoticeMessage.setUserId(storageUser.getId());
        List<StorageNoticeMessage> messages = storageNoticeMessageMapper.selectStorageNoticeMessageList(storageNoticeMessage);
        messages.forEach(item -> {
            // 处理内容中含有表情
            if (item.getContent() != null) {
                item.setContent(EmojiParser.parseToUnicode(item.getContent()));
            }
        });
        return new ServiceResponse<>(true, messages, null);
    }

    /**
     * 更新推送消息状态
     */
    @Override
    public ServiceResponse<StorageNoticeMessage> updateNoticeStatus(StorageUser storageUser, StorageNoticeMessage storageNoticeMessage) {
        storageNoticeMessageMapper.updateStorageNoticeMessage(storageNoticeMessage);
        return new ServiceResponse<>(true, storageNoticeMessageMapper.selectStorageNoticeMessageById(storageNoticeMessage.getId()), null);
    }

    /**
     * 新增系统消息
     * @param storageNoticeMessage
     * @return
     */
    @Override
    public ServiceResponse<StorageNoticeMessage> insertNoticeMessage(StorageNoticeMessage storageNoticeMessage) {
        int result = storageNoticeMessageMapper.insertStorageNoticeMessage(storageNoticeMessage);
        if (result == 1) {
            return new ServiceResponse<>(true, storageNoticeMessageMapper.selectStorageNoticeMessageById(storageNoticeMessage.getId()), null);
        }
        return new ServiceResponse<>(false, null, "新增系统消息失败");
    }

}
